package com.yyq.adms.sysmanage.action;

import javax.annotation.Resource;


import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.yyq.sos.action.BaseAction;
import com.yyq.sos.constants.ActionType;
import com.yyq.sos.pojo.SysLogs;
import com.yyq.sos.service.SysLogsBpo;

@Controller("sysLogsAction")
@Scope("prototype")
public class SysLogsAction extends BaseAction {
	private static final long serialVersionUID = -6452593784123143034L;
	private SysLogs po = new SysLogs();
	private SysLogsBpo bpo;
	

	private String[] fields;

	public String execute() throws Exception {
		super.setObj(po);
		super.setBpo(bpo);
		return super.execute();
		
	}

	public static void main(String[] ss)throws Exception{
		/*Iterator it = SessionUtil.getConfiguration().getTableMappings();
		for(;it.hasNext();){
			org.hibernate.mapping.Table obj = (org.hibernate.mapping.Table)it.next();
			System.out.println(obj+"   ------  "+obj.getName());
		}*/
	}

	public String data() {
		String sql = "select a.log_id,b.user_name,b.user_cname,a.table_name,a.action_type,a.begin_time,a.action_result " +
				" from ss_sys_logs a,ss_sys_users b where a.user_id=b.user_id ";
		if (po.getTableName() != null && !"".equals(po.getTableName())) {
			if (!"0".equals(po.getTableName()))
				sql += "  and a.table_name='" + po.getTableName() + "'";
		}
		if (po.getActionType() != null && !"".equals(po.getActionType())) {
			if (!"0".equals(po.getActionType()))
				sql += "  and a.action_type='" + po.getActionType() + "' ";
		}
		if (po.getUserName() != null && !po.getUserName().trim().equals("")) {
				sql += "  and b.user_name='" + po.getUserName().trim() + "' ";
		}
		
		sql +=" order by a.begin_time desc ";
		super.setFormData(bpo.searchSqlPage(sql, super.getPageForm().getPageNo(),
				super.getPageForm().getPageSize()));
		return ActionType.ACTION_DATA_LIST;
	}

	public String download() {
		String beginDate = ((String[])this.getFormValue("beginDate"))[0];
		String endDate = ((String[])this.getFormValue("endDate"))[0];
		System.out.println("开始备份============");
		if(bpo.backup(beginDate, endDate))
			this.setMsg("1");//日志备份成功
		else
			this.setMsg("0");//日志备份成功
		System.out.println("结束备份-----------------");
		return ActionType.ACTION_MSG;
	}

	public String upload() {
		String beginDate = ((String[])this.getFormValue("beginDate"))[0];
		String endDate = ((String[])this.getFormValue("endDate"))[0];
		System.out.println("开始备份============");
		if(bpo.resume(beginDate, endDate))
			this.setMsg("1");//日志备份成功
		else
			this.setMsg("0");//日志备份成功
		System.out.println("结束备份-----------------");
		return ActionType.ACTION_MSG;
	}

	public String preview() {
		po = (SysLogs) bpo.findByPk(po.getId());
		if (po.getActionType() != null && po.getLogContext() != null
				&& ("WRITE".equalsIgnoreCase(po.getActionType()) 
						|| "DELETE".equalsIgnoreCase(po.getActionType())
						||"UPDATE".equalsIgnoreCase(po.getActionType()))) {
			try{
				java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream();
				int len = -1;
				byte[] b = new byte[2048];
				java.io.InputStream context = po.getLogContext().getBinaryStream();
				while((len = context.read(b, 0, 2048)) != -1){
					baos.write(b, 0, len);
				}
				this.setFormValue("content", baos.toString());
				fields = baos.toString().split(";");
			}catch(Exception ex){
			}
		}
		return super.preview();
	}

	public SysLogs getPo() {
		return po;
	}

	public void setPo(SysLogs po) {
		this.po = po;
	}

	public String[] getFields() {
		return fields;
	}

	@Resource(name="sysLogsBpo")
	public void setBpo(SysLogsBpo bpo) {
		bpo.setCls(po.getClass());
		this.bpo = bpo;
	}

}
